import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '药物名称',			name: 'drug_name' },
			{ label: '英文名称',			name: 'name_en' },
			{ label: '通道号',			name: 'channel_number' },
			{ label: '所属药敏组合',		name: 'group_id',	type: 'select', loading:true, idStr:'uuid', nameStr:'group_name' },
		],
		// 提交表单列表
		formData : [
			{ label: '药物名称',		name: 'drug_name', 			half:true, required:true },
			{ label: '英文名称',		name: 'name_en', 			half:true },
			{ label: '简写',			name: 'short_name', 		half:true },
			{ label: '通道号',		name: 'channel_number', 				half:true },
			{ label: '排序',			name: 'sort', 		half:true, type:'number' },
			{ label: '描述',			name: 'description', 		full:true, type: 'textarea' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		if(window.isInit){
			this.fetch()
		}
	}
	
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 所属药敏组合
			$fn.setRow(forms, 'group_id', await f.getCache( { api:'drug-group/index' } ))
			
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'drug-sensitivity/index')
	
	// 表单
	cols = [
		{ title: '抗菌药物名称', 	dataIndex: 'drug_name', 		width:200 },
		{ title: '英文名称', 	dataIndex: 'name_en', 			width:200 },
		{ title: '描述', 		dataIndex: 'description', 		width:200 },
		{ title: '简写', 		dataIndex: 'short_name', 		width:200 },
		{ title: '通道号', 		dataIndex: 'channel_number', 	...$fn.table.num },
		{ title: '排序', 		dataIndex: 'sort', 				...$fn.table.num },
		TableControls.controls.call(this)
	]
	// 表格操作按钮
	table_controls = (text,{ uuid }) => {
		return [
			TableControls.edit.call(this, 'drug-sensitivity/info', { uuid }),				// 编辑
			this.onDel([ uuid ]),															// 删除
		]
	}
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this),			// 添加
			this.onDel(),						// 删除
		]
	}
	// 删除
	onDel = uuid => Delete.call(this, 'drug-sensitivity/del', { uuid, isKeys: !uuid, onComplete:()=>$fn.dataRemove('drug-sensitivity/index') })
	
	render(){
		return (
			<>
				<Page title='药敏' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this,{isInit:window.isInit}) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'drug-sensitivity/add', editApi:'drug-sensitivity/edit', onComplete:({ param })=>{$fn.dataClear()}}) }
			</>
		)
	}
}

export default Index